package org.example.controller;

import jakarta.annotation.PostConstruct;
import org.example.pojo.Invitation;
import org.example.pojo.Result;
import org.example.service.InvitationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/invitation")
public class InvitationController {
    // mongodb 模板
    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private InvitationService invitationService;

    /**
     * 初始化 mongodb 数据库
     */
    @PostConstruct
    private void init() {
        List<Invitation> invitations = invitationService.findInvitations(null);
        // 保存到 mongodb
        mongoTemplate.dropCollection("invitation");
        mongoTemplate.insertAll(invitations);
    }

    /**
     * 无条件查询，查询所有的帖子
     */
    @GetMapping
    public Result getInvitations() {
        // 先查 mongodb
        List<Invitation> invitations = null;
        invitations = mongoTemplate.findAll(Invitation.class);
        // 再判断 mongodb 中是否有，若无，从 mysql 中获取
        if (invitations == null || invitations.size() == 0) {
            invitations = invitationService.findInvitations(null);
            // 保存到 mongodb
            mongoTemplate.insertAll(invitations);
        }
        System.err.println("invitations1 " + invitations);
        return Result.success(invitations);
    }

    /**
     * 根据帖子的id查询，返回该帖子及回复
     */
    @GetMapping("/{invitationId}")
    public Result getInvitations(@PathVariable("invitationId") Integer invitationId) {
        // 先查 mongodb
        List<Invitation> invitations = null;
        Query query = new Query(Criteria.where("id").is(invitationId));
        invitations = mongoTemplate.find(query, Invitation.class);
        // 再判断 mongodb 中是否有，若无，从 mysql 中获取
        if (invitations == null || invitations.size() == 0) {
            invitations = invitationService.findInvitations(invitationId);
            // 保存到 mongodb
            mongoTemplate.insertAll(invitations);
        }
        System.err.println("invitations2 " + invitations);
        return Result.success(invitations);
    }

    /**
     * 添加帖子
     */
    @PostMapping
    public Result addInvitation(Invitation invitation) {
        // 插入到 mysql
        invitationService.save(invitation);
        // 获取 mongdb 中的集合
        List<Invitation> invitations = mongoTemplate.findAll(Invitation.class);
        // 判断是否有数据
        if (invitations == null) {
            invitations = new ArrayList<>();
        }
        invitations.add(invitation);
        // 添加到 mongodb invitation collection 中
        mongoTemplate.save(invitation);
        return Result.success(invitation);
    }

    /**
     * 修改帖子
     */
    @PutMapping
    public Result changeInvitation(Invitation invitation) {
        // 修改 mysql 中的
        invitationService.updateById(invitation);
        // 修改 mongodb 中的
        Query query = Query.query(Criteria.where("id").is(invitation.getId()));
        Update update = new Update();
        update.set("argument", invitation.getArgument());
        update.set("content", invitation.getContent());
        update.set("theme", invitation.getTheme());
        mongoTemplate.updateFirst(query, update, Invitation.class);
        return Result.success(invitation);
    }

    /**
     * 删除帖子
     */
    @DeleteMapping("/{invitationId}")
    public Result deleteInvitation(@PathVariable("invitationId") Integer invitationId) {
        // 删除 mysql 中的
        invitationService.removeById(invitationId);
        // 删除 mongodb 中的
        Query query = Query.query(Criteria.where("_id").is(invitationId));
        mongoTemplate.remove(query, Invitation.class);
        return Result.success(invitationId);
    }
}
